<?php


namespace app\api\controller\v1;

use app\api\model\Product as ProductModel;
use app\api\vaildate\Count;
use app\api\vaildate\IDMustBePostiveInt;
use app\lib\exception\ProductException;

class Product
{
    /*
     * 获取最新的产品
     * @url /product/recent?$count=15
     * @method GET
     * @count 大于0小于15的正整数
     * @return 一组product模型
     */
    public function getRecent($count = 15)
    {
        (new Count())->goCheck();
        $products = ProductModel::getNewProduct($count);
        if ($products->isEmpty()) {
            throw new ProductException();
        }
//        $collection=collection($products);
//        $products = $collection->hidden(['summary']);
        $products = $products->hidden(['summary']);
        return $products;
    }

    /*
     * 获取最新的产品
     * @url /product/category_id?id=1
     * @method GET
     */
    public function getAllInCategory($id)
    {

        (new IDMustBePostiveInt())->goCheck();
        $products = ProductModel::getProductByCategoryID($id);
        if ($products->isEmpty()){
            throw new ProductException();
        }
        $products = $products->hidden(['summary']);
        return $products;

    }
    /*
     * 根据id获取产品
     * @url /product/:id
     * @http GET
     */
    public function getOne($id){
        $result = ProductModel::getProductDetail($id);
        return $result;
    }
}